@extends('admin.layout.layout', ['row' => @$page])

@section('contain')
    <style>
        .table .tth{
            background-color: rgba(0, 0, 0, 0.05);
            width: 150px;
            font-weight: bold;
        }
        .table .ttd{
            width: 350px;
        }
        .table .ttt{
            padding: 0;
        }
        .ttt .table{
            margin: 0;
        }
        .ttt .table img + p{
            text-align: center;
            margin: 10px 0 0;
        }
        .tst{
            color: #00b050;
            font-weight: 700;
            text-align: center;
            font-size: 1.25rem;
        }
    </style>

    <script>
        window.PRECUT_ID='{{ $data->id }}'
    </script>

    <div class="portlet">
        <div class="portlet-title">
            <div class="caption">
                <x-portlet-breadcrumb :page="$page"></x-portlet-breadcrumb>
            </div>
            <div class="actions"></div>
        </div>
        <form action="/{{ request()->path() }}/../" method="post" onsubmit="return checkVal(this)" class="warranty-form t-right">
            @csrf
            @method('PUT')
            <input type="text" name="_previous_" value="{{ request()->header('referer') }}" hidden>
            <input type="text" name="_prec_" value="{{ $data->precut_id }}" hidden>

            <table class="table s3">
                <tbody>
                <tr>
                    <td class="tth">{{ ts('precutid') }}</td>
                    <td class="ttd">
                        <input type="text" readonly class="form-control" name="precut_id" value="{{ $data->precut_id }}">
                    </td>
                    <td class="tth">{{ ts('status') }}</td>
                    <td class="ttd">{{ ts('STAI'.$data->status) }}</td>
                    <td class="tth">{{ ts('CJR') }}</td>
                    <td class="ttd">{{ getJc($data->created_by) }}</td>
                </tr>
                <tr>
                    <td colspan="6" class="tst">{{ ts('INSTALLATIONINFORMATION') }}</td>
                </tr>
                <tr>
                    <td class="tth">{{ ts('distributor') }}</td>
                    <td>
                        <input type="text" class="form-control sdjc" disabled name="sdjc" value="{{ getDJc($data->user_id) }}">
                    </td>
                    <td class="tth">{{ ts('qudao') }}</td>
                    <td>
                        <div class="input-group">
                            <input type="number" value="{{ $data->user_id }}" name="user_id" hidden>
                            <select class="form-control roll_user_id" name="user_id" disabled>
                                <option value="{{ $data->user_id }}">{{ getJc($data->user_id) }}</option>
                            </select>
                            @if($data->status != 1)
                                <div class="input-group-prepend">
                                    <span class="btn btn-primary"  data-toggle="modal" data-target="#changeWarrantyUser">{{ ts('XG') }}</span>
                                </div>
                            @endif
                        </div>
                    </td>
                </tr>

                <tr>
                    <td colspan="6" class="tst">{{ ts('VEHICLECOVERAGE') }}</td>
                </tr>
                <tr>
                    <td colspan="6" class="ttt">
                        @include('admin.subview.coverage', ['coverages' => @getPrecutCoverages(@$data->id), 'is_precut'=>0, 'isprecut'=>1])
                        {{--<div class="container-fluid">
                            @include('admin.subview.precut_coverage', ['coverages' => getPrecutCoverages($data->id)])
                        </div>--}}
                    </td>
                </tr>
                </tbody>
            </table>

            <div class="mt-4">
                <button class="btn btn-primary">{{ ts('store') }}</button>
            </div>
        </form>

        @php
            $wUser = getUser($data->user_id);
            $wDealers = [];
            $wDTId = 0;
            if (empty($wUser->id)) {
            } else {
                if ($wUser->user_type==2) {
                    $Dealers = getDealers($wUser->id);
                    $wDealers[] = $wUser;
                    $wDTId = $wUser->id;
                }
                if ($wUser->user_type==3) {
                    $Dealers = getDealers($wUser->creator_id);
                    $wDealers[] = getUser($wUser->creator_id);
                    $wDTId = $wUser->creator_id;
                }
                foreach ($Dealers as $v) {
                    $wDealers[] = $v;
                }
            }
        @endphp
        <div class="modal fade modal-s1" id="changeWarrantyUser">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title font-green">{{ ts('xgqd') }}</h5>
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                    </div>
                    <div class="modal-body">
                        @if(session('admin.user_type')==1)
                            <div class="form-item">
                                <div class="form-label" required="">{{ ts('xzsd') }}</div>
                                <div class="form-input">
                                    <select class="form-control" name="distributor_id">
                                        @if(session('admin.user_type')==1)
                                            @foreach(getDistributors() as $v)
                                                <option value="{{ $v->id }}" {{ $wDTId == $v->id ? 'selected' : '' }}>{{ $v->abbr ?: $v->company_name }}</option>
                                            @endforeach
                                        @endif
                                    </select>
                                </div>
                            </div>
                        @endif

                        <div class="form-item">
                            <div class="form-label" required="">{{ ts('xzjxs') }}</div>
                            <div class="form-input">
                                <select class="form-control" name="dealer_id" placeholder="">
                                    @foreach($wDealers as $v)
                                        <option value="{{ $v->id }}" {{ $data->user_id == $v->id ? 'selected' : '' }}>{{ $v->abbr ?: $v->company_name }}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>
                        <div class="py-3 text-center">
                            <button type="button" class="btn btn-primary" onclick="handleChangeWarrantyUser()">{{ ts('confirm') }}</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>


@endsection

@section('script')
    <script>

        $(document).ready(function () {
            initCoverageRowsV2();
        })

        $('body').on('blur change', '.precut-coverages .coverage-list .item [name="roll_number[]"]', function () {
            var val = $(this).val();
            val = String(val).trim()
            var $that = $(this);
            if (val) {
                request.get('/admin/ajax/getRestlen', {
                    params: {
                        q: val,
                        t: 'precut',
                        id: typeof PRECUT_ID != 'undefined' && PRECUT_ID || 0
                    }
                })
                    .then( function(d) {
                        var data = d.data
                        $that.parents('.item').find('.residue-length b').attr('data-length', data.restlen)
                        $that.parents('.item').find('.residue-length').removeClass('d-none').show()
                        $that.parents('.item').find('[name="film_type_id[]"]').val(data.film_type_id)
                        $that.parents('.item').find('[name="width[]"]').val(data.width)
                        updatePVCRestlen()
                    })
                    .catch(function (d) {
                        console.log('catch', d)
                        toastr.error(d.msg)
                        if (d.error_code == 1004) {
                            // $that.val(' ').focus()
                        }
                        $that.parents('.item').find('.residue-length b').attr('data-length', 0)
                        $that.parents('.item').find('.residue-length').addClass('d-none')
                    })
            }
        })
        $('body').on('change input propertychange', '.precut-coverages .coverage-list .item [name="length[]"]', function () {
            var val = $(this).val()
            var roll_number = $(this).parents('.item').find('[name="roll_number[]"]').val()
            var max = $(this).parents('.item').find('.residue-length b').attr('data-length') || 0
            var used_length = getSameRollNumberUsedLength(roll_number) || 0
            val = Number(val) || 0
            max = Number(max) || 0
            residue_max = max-used_length+val
            console.log(max, used_length)
            if (val > residue_max) {
                val = residue_max
            }
            if (val < 0) {
                val = 0
            }
            $(this).val(val)
            updateRollNumberResidueLength()
        })
        $('body').on('click', '.precut-coverages .addmore', function () {
            makeNewPVCRow();
        })

        $('body').on('click', '.precut-coverages .coverage-list .item i.delete', function () {
            var len = $(this).parents('.item').siblings('.item').length
            if (len > 0) {
                $(this).parents('.item').remove();
            }
        })
        $('body').on('change', '[name="sdid"]', function () {
            var val=$(this).val();
            var $that = $(this)
            request.get('/admin/ajax/getUsers', {
                params: {
                    t: 'd',
                    user_id: val
                }
            })
                .then( function(d) {
                    var data = d.data
                    $('.portlet select[name="user_id"]').empty().append(makeSelectOptionsOnly(data))
                })
                .catch(function (d) {
                    toastr.error(d.msg)
                })
        })
        $(document).ready(function () {
            if ($('[name="sdid"]').length >= 1) {
                request.get('/admin/ajax/getUsers', {
                    params: {
                        t: 'd',
                        user_id: $('[name="sdid"]').val()
                    }
                })
                    .then( function(d) {
                        var data = d.data
                        $('.portlet select[name="user_id"]').empty().append(makeSelectOptionsOnly(data))
                    })
                    .catch(function (d) {
                        toastr.error(d.msg)
                    })
            }
        })
    </script>
@endsection